/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.tools;

import java.awt.FileDialog;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import dgbgui.DGBGUI;
import dgbgui.command.CommandStack;
import dgbgui.logging.ErrorLogger;
import dgbgui.modeldocument.ModelDocument;
import dgbgui.modeldocument.ModelDrivenWorkspace;
import dgbgui.modeldocument.ResultStore;

public class SaveTool implements Tool {
    
    public SaveTool(String fileName) {
        m_fileName = fileName;
    }
    
    public SaveTool() {
        // No filename = Save As
        m_fileName = null;
    }

    public void execute() {
        
        // If no filename, get the filename via a dialog
        if ( m_fileName == null ) {
            FileDialog dialog = new FileDialog(DGBGUI.getInstance().getMainFrame(),"Save",FileDialog.SAVE);
            dialog.setFilenameFilter(new ModelFileFilter("dgm"));
            dialog.pack();
            DGBGUI.centerDialog(dialog);
            dialog.setVisible(true);
            // Cancelling the dialog return a null filename
            if ( dialog.getFile() != null ) {
                m_fileName = dialog.getDirectory() + dialog.getFile();
            }
        }
        
        // Only save if there's a valid filename
        if ( m_fileName != null ) {
            
            // Add the .dgm extension if it isn't present
            if ( !m_fileName.endsWith(".dgm") ) {
                m_fileName += ".dgm";
            }
            
            try {
                ObjectOutputStream stream = null;
                try {
                    stream = new ObjectOutputStream(
                            new BufferedOutputStream(
                                    new FileOutputStream(m_fileName)));

                    // Stream the model document, result store and model-driven workspace
                    ResultStore.getInstance().writeToStream(stream);
                    ModelDocument.getInstance().writeToStream(stream);
                    ModelDrivenWorkspace.getInstance().writeToStream(stream);
                    
                } catch (FileNotFoundException e) {
                    ErrorLogger.getInstance().logString("Couldn't find file " + m_fileName + " for writing.");
                }
                if ( stream != null ) {
                    stream.close();
                }
            } catch (IOException e) {
                ErrorLogger.getInstance().logException(e);
            }
            
            // Set the filename of the model
            ModelDocument.getInstance().setFileName(m_fileName);
            // Set the save point
            CommandStack.getInstance().setSavePoint();
        }
    }
    
    String m_fileName;

}
